﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmInstallInformationDetailEdit : Form
    {
        DataBase userDB = new DataBase();

        /// <summary>
        /// 存储从主页面传入的参数
        /// </summary>
        public string _ass_no;      //主数据编号
        public string _ass_no_sub;  //子表数据编号

        /// <summary>
        /// 构造函数接收主页传入的参数
        /// </summary>
        /// <param name="ass_no_sub"></param>
        /// <param name="ass_no"></param>
        public FrmInstallInformationDetailEdit(string ass_no_sub, string ass_no)
        {
            this._ass_no_sub = ass_no_sub;
            this._ass_no = ass_no;
            InitializeComponent();
        }

        /// <summary>
        /// 初始化字表编辑页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmInstallInformationDetailEdit_Load(object sender, EventArgs e)
        {
            InitDataBind();
            if (!string.IsNullOrEmpty(_ass_no_sub))
            {
                this.Text = "修改安装信息子表数据-【" + _ass_no_sub + "】";
                string sqlstr = "select * from T_assemble_sub where ass_no='" + this._ass_no_sub + "'";
                DataTable dataTable = userDB.show(sqlstr);
                if (dataTable.Rows.Count > 0)
                {
                    DataRow dr = dataTable.Rows[0];
                    this.txtAss_no.Text = dr["Ass_no"].ToString();
                    this.txtAss_num.Text = dr["Ass_num"].ToString();
                    this.cbbPro_code.SelectedValue = dr["Pro_code"].ToString();
                    this.dtpRec_sub_date.Value = Convert.ToDateTime(dr["Rec_sub_date"].ToString());
                    this.txtAss_no_sub.Text = dr["Ass_no_sub"].ToString();
                }

            }
            else
            {
                this.txtAss_no.Text = _ass_no;
                this.Text = "新增安装信息子表数据";
            }
        }


        private void InitDataBind()
        {
            //加油站表
            string S_product_sql = "select * from S_product";
            DataTable dt_S_product = userDB.show(S_product_sql);
            this.cbbPro_code.ValueMember = "Pro_code";
            this.cbbPro_code.DisplayMember = "Pro_name";
            this.cbbPro_code.DataSource = dt_S_product;

        }

        /// <summary>
        /// 保存时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlstr = "";
                if (!string.IsNullOrEmpty(_ass_no_sub))
                {
                    sqlstr = string.Format("update T_assemble_sub set Pro_code='{2}',Ass_num='{3}',Rec_sub_date='{4}' where Ass_no_sub='{5}'",
                        this.txtAss_no.Text, this.txtAss_no_sub.Text, this.cbbPro_code.SelectedValue, this.txtAss_num.Text, this.dtpRec_sub_date.Value.ToString("yyyy-MM-dd HH:mm:ss"), this.txtAss_no_sub.Text);
                }
                else
                {
                    sqlstr = string.Format("insert into T_assemble_sub(Ass_no,Ass_no_sub,Pro_code,Ass_num,Rec_sub_date) "
                        + " values('{0}','{1}','{2}','{3}','{4}')",
                       this.txtAss_no.Text,
                       this.txtAss_no_sub.Text,
                       this.cbbPro_code.SelectedValue,
                       this.txtAss_num.Text,
                       this.dtpRec_sub_date.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (userDB.dosqlcom(sqlstr) > 0)
                {
                    this.DialogResult = DialogResult.OK;
                    MessageBox.Show("保存成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常！" + ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
